package com.atguigu.guli.edu.service.impl;

import com.atguigu.common_utils.utils.PageUtils;
import com.atguigu.common_utils.utils.Query;
import com.atguigu.guli.edu.dao.EduCourseDescriptionDao;
import com.atguigu.guli.edu.dao.EduSubjectDao;
import com.atguigu.guli.edu.entity.Course;
import com.atguigu.guli.edu.entity.CourseDescription;
import com.atguigu.guli.edu.entity.Teacher;
import com.atguigu.guli.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.edu.entity.query.CourseInfoSearch;
import com.atguigu.guli.edu.entity.query.CourseQuery;
import com.atguigu.guli.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.base.dto.CourseInfoDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.guli.edu.dao.EduCourseDao;
import com.atguigu.guli.edu.service.EduCourseService;
import org.springframework.util.StringUtils;


@Service("eduCourseService")
public class EduCourseServiceImpl extends ServiceImpl<EduCourseDao, Course> implements EduCourseService {

    @Autowired
    EduCourseDao courseDao;
    @Autowired
    EduCourseDescriptionDao courseDescriptionDao;
    @Autowired
    EduTeacherServiceImpl teacherService;
    @Autowired
    EduSubjectServiceImpl subjectService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Course> page = this.page(
                new Query<Course>().getPage(params),
                new QueryWrapper<Course>()
        );

        return new PageUtils(page);
    }

    @Override
    public String  saveInfo(CourseInfoForm courseInfoForm) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        //保存课程信息
        courseDao.insert(course);
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(course.getId());
        //保存课程简介
        courseDescriptionDao.insert(courseDescription);
        return course.getId();
    }

    @Override
    public void updateInfo(CourseInfoForm courseInfoForm,String courseId) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        //保存课程信息
        course.setId(courseId);
        courseDao.updateById(course);
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseId);
        //保存课程简介
        courseDescriptionDao.updateById(courseDescription);
    }

    @Override
    public CourseInfoForm selectById(String id) {
        //查询课程信息
        Course course = courseDao.selectById(id);
        //查询讲师信息
        //Teacher byId = teacherService.getById(course.getTeacherId());
        //查询课程简介
        CourseDescription description = courseDescriptionDao.selectById(course.getId());
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course,courseInfoForm);
        courseInfoForm.setDescription(description.getDescription());


        return courseInfoForm;
    }

    @Override
    public Page<CourseInfoVo> pageList(Integer currPage, Integer pagesize, CourseInfoSearch courseInfoSearch) {
        Page<CourseInfoVo> page = new Page<>(currPage,pagesize);
        //课程id
        String subjectId = courseInfoSearch.getSubjectId();
        //讲师id
        String teacherId = courseInfoSearch.getTeacherId();
        //课程父id
        String subjectParentId = courseInfoSearch.getSubjectParentId();
        //课程标题
        String title = courseInfoSearch.getTitle();

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("t1.subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("t1.teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("t1.subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("t1.title",title);
        }

        List<CourseInfoVo> courseInfoVos = courseDao.selectListInfo(page,queryWrapper);
        return page.setRecords(courseInfoVos);
    }

    @Override
    public CourseInfoVo getByIdCourseInfo(String id) {
        CourseInfoVo courseInfoVo = courseDao.getByIdCourseInfo(id);
        return courseInfoVo;
    }

    @Override
    public List<Course> listCourse(CourseQuery courseQuery) {
        String title = courseQuery.getTitle();//标题
        String subjectParentId = courseQuery.getSubjectParentId();//父id
        String cloum = courseQuery.getCloum();//按条件查询
        int type = courseQuery.getType();//升序or降序

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        //判断父id是否为空
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id",subjectParentId);
        }
        //判断课程是否为空
        if (!StringUtils.isEmpty(title)){
            queryWrapper.eq("title",title);
        }
        //判断查询条件
        if (!StringUtils.isEmpty(cloum)){
            if (type==0){
                queryWrapper.orderByDesc(cloum);
            }else {
                queryWrapper.orderByDesc(cloum);
            }
        }

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CourseDetailsVo getCoursesDetails(String courseid) {
        CourseDetailsVo courseDetailsVo = courseDao.selectCourseDatilsVoByCourseId(courseid);
        //获取浏览次数
        Long viewCount = courseDetailsVo.getViewCount();
        Course course = new Course();
        course.setId(courseid);
        course.setViewCount(viewCount+1);
        this.baseMapper.updateById(course);
        return courseDetailsVo;
    }

    @Override
    public CourseInfoDto getOrderCourse(String courseId) {
        Course course = this.getById(courseId);
        Teacher teacher = teacherService.getById(course.getTeacherId());
        CourseInfoDto courseInfoDto = new CourseInfoDto();
        BeanUtils.copyProperties(course,courseInfoDto);
        courseInfoDto.setTeacherName(teacher.getName());
        return courseInfoDto;
    }

    @Override
    public void updateCourseByCount(String courseId) {
        Course course = getById(courseId);
        //增加销量
        course.setBuyCount(course.getBuyCount()+1);
        updateById(course);
    }

}